package org.example.test915.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiOperation;
import org.example.common.bean.CommonPage;
import org.example.common.bean.ResponseBean;
import org.example.test915.entity.bo.PageUserInfoParam;
import org.example.test915.entity.bo.UserInfoParam;
import org.example.test915.entity.po.UserInfoPO;
import org.example.test915.entity.rt.ExeclEntity;
import org.example.test915.entity.vo.UserInfoVO;
import org.example.test915.service.UserInfoService;
import org.example.util.ExcelUtils;
import org.jetbrains.annotations.TestOnly;
import org.junit.Test;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/myTest")
public class TestLogicDeleteController {

    @Resource
    private UserInfoService userInfoService;

    @PostMapping("/saveOrUpdate")
    @ApiOperation("新增或修改")
    public ResponseBean<Void> saveOrUpdate(@RequestBody @Valid UserInfoParam param) {
        // 测试
        userInfoService.saveOrUpdate(param);
        return ResponseBean.success();
    }


    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseBean<Void> delete(Long id) {
        userInfoService.delete(id);
        return ResponseBean.success();
    }

    @GetMapping("/query")
    @ApiOperation("查询")
    public ResponseBean<UserInfoVO> query(Long id) {
        return ResponseBean.success(userInfoService.queryById(id));
    }


    @PostMapping("/page")
    @ApiOperation("分页查询")
    public ResponseBean<CommonPage<UserInfoVO>> page(@RequestBody PageUserInfoParam param) {
        return ResponseBean.success(userInfoService.page(param));
    }

    @ApiOperation("从Excel导入")
    @PostMapping(value = "/excelImport")
    public ResponseBean<List<ExeclEntity>> importMemberList(@RequestPart("file") MultipartFile file) throws IOException {
        //ExeclEntity 需有无参构造函数
        List<ExeclEntity> memberList = ExcelUtils.read(file.getInputStream(), ExeclEntity.class, 1);
        //插入数据

        return ResponseBean.success(memberList);
    }

    @Test
    public static void main(String[] args) {
        UserInfoPO userInfoPO = new UserInfoPO();
        String jsonStr = "[\"苹果\", \"香蕉\", \"橙子\"]";
        // 解析为 String 列表
        List<String> fruitList = JSONUtil.parseArray(jsonStr).toList(String.class);
        JSON parse = JSONUtil.parse(userInfoPO);
        JSONObject json = JSONUtil.parseObj(userInfoPO);
//        JSONUtil.toList();
//        Convert.toList()
        String[] array = {"A"};
        List<String> list1 = Convert.toList(String.class, array);
//        System.out.println("数组转List: " + list1); // [A, B, C]


        Map<String,Object> map = new HashMap<>();
        map.put("a","1");
        map.put("b","2");
        map.put("c","3");
        String jsonStr1 = JSONUtil.toJsonStr(map);
        System.out.println(jsonStr1);


        Map<String,Object> mapp = JSONUtil.toBean(jsonStr1, Map.class);
        System.out.println(mapp);
        Map<String, Object> map1 = JSONUtil.parseObj(jsonStr1);
        String str = Convert.toStr(map1.get("a"));
        System.out.println(map1);

        JSONObject jsonObject = JSONUtil.parseObj(jsonStr1);
        System.out.println(jsonObject);

    }
}
